/**   
* @Description: OpenReplicator配置文件    
*/ 
package org.shanktech.tshare.binlogsync.config;

import java.util.Arrays;
import java.util.List;

import org.shanktech.tshare.binlogsync.listener.NotificationListener;
import org.shanktech.tshare.binlogsync.util.Constant;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.google.code.or.OpenReplicator;

/** 
* @author sztan nanjing_xiaotan@163.com 
* @date 2017年6月25日 上午12:29:13 
* @version V1.0   
*/
@Configuration
public class OpenReplicatorConfig {
  
  @Value("${binlog.db.listern-databases}")
  private String listernDatabases;

  @Value("${binlog.db.listern-tables}")
  private String listernTables;

  @Value("${binlog.db.port}")
  private int port;
  
  @Value("${binlog.db.host}")
  private String host;
  
  @Value("${binlog.db.user}")
  private String user;
  
  @Value("${binlog.db.password}")
  private String password;
  
  @Value("${binlog.db.server-id}")
  private int serverId = 1;
  
  @Value("${binlog.db.binlog-file-name}")
  private String binlogFileName;
  
  @Value("${binlog.db.binlog-position}")
  private long binlogPosition = 4;
  
  @Value("${binlog.db.binlog-model}")
  private String binlogModel = Constant.BinlogFormat.ROW.getValue();
  
  
  @Bean
  public OpenReplicator getInstance() {

    List<String> tables = Arrays.asList(listernTables.split(","));
    List<String> databases = Arrays.asList(listernDatabases.split(","));

    OpenReplicator or = new OpenReplicator();
    or.setUser(user);
    or.setPassword(password);
    or.setHost(host);
    or.setPort(port);
    or.setServerId(serverId);
    or.setBinlogPosition(binlogPosition);
    or.setBinlogFileName(binlogFileName);
    Constant.BinlogFormat binlogFormat = Constant.BinlogFormat.ROW;
    if (Constant.BinlogFormat.MIXED.getValue().equalsIgnoreCase(binlogModel)) {
      binlogFormat = Constant.BinlogFormat.MIXED;
    }
    or.setBinlogEventListener(
        new NotificationListener(binlogFormat, databases, tables));
    return or;
  }
  
}
